var currProjectId = "";
layui.config({
    base: '../lib/layui/lay/modules/',
}).use(['element','table','form','layer','laydate','tree'], function(){
    var table = layui.table,
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    layer = layui.layer;
    var tree = layui.tree;

    loadDictList('carType');

    function loadDictList(dictType) {
        $.get('/cardpassmgrCard/getDictItems', {dictType:dictType}, function (result) {
            if(result != null){
                if(result.data != null){
                    var a$opt_html = "<option value=''>全部</option>";
                    $.each(result.data, function (index, item) {
                        a$opt_html += "<option value='" + item.dictKey + "'>" + item.dictName + "</option>";
                    });
                    $("#searchTypeId").empty().append(a$opt_html);
                    form.render('select');
                }
            }
        });
    }

    $.get('/cardpassmgrProject/listAll',{},function (res) {
        if(res.status === 200){
            var options = res.data;
            tree.render({
                elem: '#projectTree'
                ,data: options
                ,edit: ['add', 'update', 'del'] //操作节点的图标
                ,click: function(obj){
                    var projectId = obj.data.id;
                    currProjectId= projectId;
                    queryRecord();
                },operate: function(obj){
                    var type = obj.type; //得到操作类型：add、edit、del
                    var data = obj.data; //得到当前节点的数据
                    var elem = obj.elem; //得到当前节点元素
                    //Ajax 操作
                    var id = data.id; //得到节点索引
                    if(type === 'add'){ //增加节点
                        //返回 key 值
                        var newId = new Date().getTime();
                        var param = {id:newId,name:"未命名",parentId:id,isUpdate:false};
                        var success = ajaxPost('/cardpassmgrProject/saveOrUpdate',param);
                        if(!success){
                            return false;
                        }
                        return id;
                    } else if(type === 'update'){ //修改节点
                        //得到修改后的内容
                        var newTitle = elem.find('.layui-tree-txt').html();
                        var param = {id:id,name:newTitle,isUpdate:true};
                        ajaxPost('/cardpassmgrProject/saveOrUpdate',param);
                    } else if(type === 'del'){ //删除节点
                        var param = {id:id};
                        ajaxPost('/cardpassmgrProject/delete',param);
                    }
                }
            });
        }
    });

    function ajaxPost(url,param){
        $.post(url,param,function (res) {
            if(res.status === 200){
                layer.msg(res.msg,{});
                return true;
            }else{
                layer.msg(res.msg);
                return false;
            }
        });
    }

    //执行一个laydate实例
    var beginDateInit = function(startId,endId ){
        laydate.render({
            elem: startId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date(value).getTime();
                var endTime = new Date($(endId).val()).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(startId).val('');
                }
            }
        })
    }
    var endDateInit = function(startId,endId ){
        laydate.render({
            elem: endId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date($(startId).val()).getTime();
                var endTime = new Date(value).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(endId).val('');
                }
            }
        })
    }

    // 开门记录时间
    beginDateInit('#openBeginDate','#openEndDate');
    endDateInit('#openBeginDate','#openEndDate');

    table.render({
        elem: '#acsRecordTable'
        ,url:'/cardpassmgrRecord/list'
        ,title: '通行记录'
        ,where: {}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'cardNum', title:'通行卡号',align:'center'}
            ,{field:'type', title:'车辆类型',align:'center'}
            ,{field:'carNum', title:'车牌号',align:'center'}
            ,{field:'passTime', title:'通行时间',  sort: true,templet:
                    function(data){
                        return formatTime(data.passTime);
                }}
            ,{field:'direction', title:'进出方向', templet:
                function(data){
                    return parseDirection(data.direction);
                }}
        ]]
        ,page: true
    });

    function formatTime(data){
        return data ? data.replace("T", " "):"";
    }

    function queryRecord(){
        var cardNum = $("#searcheCardNum").val();
        var typeId = $("#searchTypeId").val();
        var carNum = $("#searcheCarNum").val();
        var direction = $("#direction").val();
        var startDate= $("#openBeginDate").val();
        var endDate= $("#openEndDate").val();
        table.reload('acsRecordTable',{
            where: {projectId:currProjectId,cardNum:cardNum,typeId:typeId,
                carNum:carNum,direction:direction,startDate:startDate,endDate:endDate}
            ,page:  {
                curr:1
            }
        });
    }

    form.on('submit(query_record)',function () {
        queryRecord();
        return false;
    });

});

function parseDirection(direction) {
    if(direction){
        switch (direction) {
            case 1 : return "进";
            case 2 : return "出";
            default: return "未知";
        }
    }
    return "未知";
}
function reload(){
    location.reload();
}
